
package com.factory.test.base;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.face.sdk.yx5821.YX5821SdkServiceImpl;
import com.factory.test.BaseFactoryTest;
import com.factory.test.bean.Design;
import com.factory.test.util.DataSaveUtil;
import com.yecontech.sdk.base.YTSdkService;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import me.jessyan.autosize.AutoSize;
import me.jessyan.autosize.AutoSizeConfig;


public class MyApplication extends Application {

    private static MyApplication myApplication;
    public static MyApplication getMyApplication(){
        return myApplication;
    }

    private static BaseFactoryTest deviceFactoryTest;

    @Override
    public void onCreate() {
        super.onCreate();
        myApplication = this;
        initCrachLog();                     //初始化异常日志保存工具
        Toasty.Config.getInstance()
                .allowQueue(false)          // optional (prevents several Toastys from queuing)
                .setTextSize(18)
                .apply();                   // required

        reStartAutoSize();                  //初始化


        try {
//            Class<?> c = Class.forName(getFactoryImpl());
//            Constructor<?> constructor = c.getConstructor(Context.class);
//            deviceFactoryTest = (BaseFactoryTest) constructor.newInstance(this);
            Log.i("application", "FactoryTest newInstance success");
        } catch (Exception e) {
            Log.e("application", "FactoryTest impl newInstance fail", e);
        }
    }

    public static YTSdkService getSdkService(Context context){
        return new YX5821SdkServiceImpl(context);
    }


    public static BaseFactoryTest getBaseFactoryTest(){
        return deviceFactoryTest;
    }

    public static void reStartAutoSize(){
        int index = DataSaveUtil.getInstance().getDesignIndex();
        Design design = getDesignList().get(index);
        AutoSize.checkAndInit(myApplication);
        AutoSizeConfig.getInstance()
                .setDesignWidthInDp(design.getDesignWidthInDp())
                .setDesignHeightInDp(design.getDesignHeightInDp())
                .restart();
    }


    private static List<Design> getDesignList(){
        List<Design> list = new ArrayList<>();
        list.add(new Design("竖屏模式", 540, 960));
        list.add(new Design("宽屏模式", 1280, 720));
        return list;
    }


    /**
     * attachBaseContext 生命周期 优先于 onCreate
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
        myApplication = this;
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    private void initCrachLog() {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
    }


    /**
     * 判断是否有网络连接
     * @param context
     * @return
     */
    public boolean isNetworkConnected(Context context) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()){	//判断网络连接是否打开
            return  mNetworkInfo.isConnected();
        }
        return false;
    }


    /**
     * 判断当前apk是否为debug
     * @return
     */
    public static boolean isApkDebugable() {
        try {
            ApplicationInfo info = MyApplication.getMyApplication().getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception x) {
            return false;
        }
    }




    /**
     * 获取软件版本号 : <b>versionCode<b>
     * @return
     */
    public int getAppVersionCode() {
        int versionCode = 0;
        try {
            PackageManager packageManager = getPackageManager();
            versionCode = packageManager.getPackageInfo(getPackageName(), 0).versionCode;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取软件版本号名称:<b> versionName<b>
     * @return
     */
    public String getAppVersionName() {
        String versionName = "";
        try {
            PackageManager packageManager = getPackageManager();
            versionName = packageManager.getPackageInfo(getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }


    private String channelName;
    private String factoryImpl;
    public String getChannelName(){
        if(channelName != null){
            return channelName;
        }

        try {
            PackageManager packageManager = getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData.getString("CHANNEL_VALUE", "");
                        return channelName;
                    }
                }
            }
        } catch (Exception e) {
            Log.e("sdk", "getChannelName fail ", e);
        }

        return "";
    }
    public String getFactoryImpl(){
        if(factoryImpl != null){
            return factoryImpl;
        }

        try {
            PackageManager packageManager = getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        factoryImpl = applicationInfo.metaData.getString("FACTORY_IMPL", "");
                        return factoryImpl;
                    }
                }
            }
        } catch (Exception e) {
            Log.e("sdk", "getFactoryImpl fail ", e);
        }

        return "";
    }
}
